$(function () {
    var win_width = $(window).width();
    var win_height = $(window).height();
    $("#chartmainbar").css({
        "width": win_height*0.9,
        "height": win_height*0.8
    });
	vm.init();
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        userList:[],
		q: {
			userId:null
		},
		lastHealthData:{weightDifference:'',bodyfatDifference:''},
		chartmainbar:null,
		optionbar : {
			    title : {
			        text: '健康状况',
			        subtext: ''
			    },
			    tooltip : {
			        trigger: 'axis'
			    },
			    legend: {
			        data:['体重','体脂','内脏脂肪']
			    },
			    toolbox: {
			        show : true,
			        feature : {
			            mark : {show: true},
			            dataView : {show: true, readOnly: false},
			            magicType : {show: true, type: ['line', 'bar']},
			            restore : {show: true},
			            saveAsImage : {show: true}
			        }
			    },
			    calculable : true,
			    xAxis : [
			        {
			            type : 'category',
			            data : [],
			        }
			    ],
			    yAxis : [
	                {
	                    'type':'value',
	                    'name':'体重',
	                    interval: 1
	                },
	                {
	                    'type':'value',
	                    'name':'体脂',
	                },
	                {
	                    'type':'value',
	                    'name':'内脏脂肪',
	                }
			    ],
			    series : [
			        {
			            name:'体重',
			            type:'line',
			            data:[],
			            yAxisIndex:0,
			            markPoint : {
			                data : [
			                    {type : 'max', name: '最大值'},
			                    {type : 'min', name: '最小值'}
			                ]
			            },
			            markLine : {
			                data : [
			                    {type : 'average', name: '平均值'}
			                ]
			            },
			            itemStyle : { normal: {label : {show: true}}}
			        },
			        {
			            name:'体脂',
			            type:'line',
			            data:[],
			            yAxisIndex:1,
			            markPoint : {
			                data : [
			                    {type : 'max', name: '最大值'},
			                    {type : 'min', name: '最小值'}
			                ]
			            },
			            markLine : {
			                data : [
			                    {type : 'average', name: '平均值'}
			                ]
			            },
			            itemStyle : { normal: {label : {show: true}}}
			        },
			        {
			            name:'内脏脂肪',
			            type:'line',
			            data:[],
			            yAxisIndex:1,
			            markPoint : {
			                data : [
			                    {type : 'max', name: '最大值'},
			                    {type : 'min', name: '最小值'}
			                ]
			            },
			            markLine : {
			                data : [
			                    {type : 'average', name: '平均值'}
			                ]
			            },
			            itemStyle : { normal: {label : {show: true}}}
			        }
			    ]
			}
	},
	methods: {
		selectUser:function(userId){
			vm.q.userId = userId;
			vm.drawLine();
		},
		init:function(){
            Ajax.request({
			    url: "../user/queryAll",
                type: "POST",
                successCallback: function (r) {
            		vm.userList = r.list;
				}
			});
		},
		drawLine: function(){
            Ajax.request({
                url: "../mihealthdata/report/"+vm.q.userId,
                type: "POST",
                params: {},
                async: true,
                successCallback: function (r) {
                	if(r.code == 0){
                		vm.optionbar.xAxis[0].data = [];
                		
                		vm.optionbar.series[0].data = [];
                		vm.optionbar.series[1].data = [];
                		vm.optionbar.series[2].data = [];
                		if(r.data.length == 0){
                	        chartmainbar = echarts.init(document.getElementById("chartmainbar"));
                	        alert("无数据！");
                		}
                		for(var i=0;i<r.data.length;i++){
                    		vm.optionbar.xAxis[0].data.push(r.data[i].createDate);;
                    		vm.optionbar.series[0].data.push(r.data[i].weight);
                    		vm.optionbar.series[1].data.push(r.data[i].bodyfat);
                    		vm.optionbar.series[2].data.push(r.data[i].visceralFat);
                		}
            	        //绘制图表
            	        chartmainbar = echarts.init(document.getElementById("chartmainbar"));
            	        chartmainbar.setOption(vm.optionbar);
                	}else{
                		alert(r.msg);
                	}
                }
            });
	    }  
	}
});
